package com.tencent.sr.iris.activity.service.constant;

import java.time.LocalDate;

/**
 * @author nixiaolin
 * @date 2023/2/17 14:35
 */
public final class ActivityRedisKey {

    /**
     * 批量设置key并设置失效时间数据脚本
     * ARGV列表的最后一个参数为过期时间
     */
    public static final String BATCH_EXPIRE_SET_SCRIPT = "local len = #KEYS\n" +
            "local exSeconds = ARGV[len + 1]\n" +
            "for i, v in ipairs(KEYS) do\n" +
            "   redis.call('SETEX', v, exSeconds, ARGV[i])\n" +
            "end\n" +
            "return true";

    public static final String ACTIVITY_CACHE_KEY = "iris:activity:cache:%s";

    public static final String ACTIVITY_TASK_CACHE_KEY = "iris:activity:task:cache:%s";

    public static final String TASK_AWARD_CACHE_KEY = "iris:activity:task:award:cache:%s:%s";

    public static final String USER_AWARD_POPUP_LOCK_KEY = "user:award:popup:lock:%s";

    public static final String USER_AWARD_TIME_KEY = "user:award:tag:%s:%s:%s";

    public static final String USER_AWARD_POPUP_TAG_KEY = "user:award:popup:tag:%s:%s:%s";

    public static final String USER_OPEN_APP_TAG_KEY = "user:open:app:tag:key:%s:%s";

    public static final String INVITE_TASK_LOCK_KEY = "IrisInviteBecomeTraineeTaskExecutor_lock_%s";

    public static final String TASK_AWARD_ACTIVITY_TASK_ESTIMATED_CACHE_KEY = "iris:activity:task:award:cache:estimated:%s:%s";

    public static final String INVITE_TASK_NEW_LOCK_KEY = "IrisInviteNewTaskExecutor_lock_%s";

    /**
     * 邀请有礼限额缓存 月份yyyyMM uid
     */
    public static final String ACTIVITY_INVITE_LIMIT_KEY = "iris:activity:invite:limit:%s:%s";

    /**
     * 月月领是否更新key
     */
    public static final String ACTIVITY_MONTH_GIFT_IS_UPDATED_KEY = "iris:activity:task:month:gift:update:%s";

    /**
     * 月月领锁key
     */
    public static final String ACTIVITY_MONTH_GIFT_LOCKED_KEY = "iris:activity:task:month:gift:locked";

    /**
     * 发奖任务记录信息
     */
    public static final String AWARD_TASK_RECORD_INFO = "iris:activity:award:task:info:%s:%s";


    /**
     * 用户上报打开小程序事件上报缓存key
     *
     * @param uid
     * @return
     */
    public static String getUserOpenAppTagKey(String uid) {
        String userOpenAppTagKey = String.format(ActivityRedisKey.USER_OPEN_APP_TAG_KEY, uid, LocalDate.now());
        return userOpenAppTagKey;
    }

    public static String getInviteTaskLockKey(String pid) {
        return String.format(ActivityRedisKey.INVITE_TASK_LOCK_KEY, pid);
    }

    public static String getInviteTaskNewLockKey(String pid) {
        return String.format(ActivityRedisKey.INVITE_TASK_NEW_LOCK_KEY, pid);
    }

    /**
     * 用户发券弹窗标识key
     *
     * @param uid
     * @param taskId
     * @return
     */
    public static String getUserAwardPopupTagKey(String uid, String taskId) {
        return String.format(ActivityRedisKey.USER_AWARD_POPUP_TAG_KEY, uid, taskId, LocalDate.now());
    }

    /**
     * 用户发券弹窗锁key
     *
     * @param uid
     * @return
     */
    public static String getUserAwardPopupLockKey(String uid) {
        return String.format(ActivityRedisKey.USER_AWARD_POPUP_LOCK_KEY, uid);
    }

    /**
     * 用户发券时间key
     *
     * @param uid
     * @param taskId
     * @param taskPeriod
     * @return
     */
    public static String getUserAwardTimeKey(String uid, String taskId, String taskPeriod) {
        return String.format(ActivityRedisKey.USER_AWARD_TIME_KEY, uid, taskId,
                taskPeriod);
    }

    /**
     * 月月领是否更新key
     *
     * @param month 月份字符串
     * @return RedisKey
     */
    public static String getIsUpdateMonthGiftKey(String month) {
        return String.format(ActivityRedisKey.ACTIVITY_MONTH_GIFT_IS_UPDATED_KEY, month);
    }

    /**
     * 发奖任务记录信息key
     *
     * @param uid
     * @param taskBizId
     * @return
     */
    public static String getAwardTaskRecordInfoKey(String uid, String taskBizId) {
        return String.format(ActivityRedisKey.AWARD_TASK_RECORD_INFO, uid, taskBizId);
    }

}
